#include <stdio.h>
#include <stdlib.h>

  struct list {
    struct list* next;
    struct list* prev;
    int data;
  };

  void init(struct list**);
  void add_data(struct list*,int);
  void display(struct list*);
  void del_data(struct list*,int);
  void del_list(struct list*);

  int main(int argc, char *argv[])
  {
    struct list* head;
    init(&head);
    add_data(head,1);
    add_data(head,2);
    add_data(head,3);
    add_data(head,4);
    add_data(head,5);
    add_data(head,6);
    add_data(head,7);
    add_data(head,8);
    add_data(head,9);
    add_data(head,10);
    add_data(head,11);
    add_data(head,12);
    add_data(head,13);
    add_data(head,14);
    add_data(head,15);
    add_data(head,16);
    add_data(head,17);
    add_data(head,18);
    add_data(head,19);
    add_data(head,20);
    add_data(head,21);
    display(head);
    del_data(head,1);
    del_data(head,2);
    del_data(head,3);
    del_data(head,4);
    del_data(head,5);
    del_data(head,6);
    del_data(head,7);
    del_data(head,8);
    del_data(head,9);
    del_data(head,10);
    del_data(head,11);
    del_data(head,12);
    display(head);
    display(head);
    return 0;
  }

  void init(struct list** head)
  {
    *head = (struct list*)malloc(sizeof(struct list));
    (*head)->next = (*head)->prev = *head;
  }

  void add_data(struct list* head,int data)
  {
    struct list* new_node;
    new_node = (struct list*)malloc(sizeof(struct list));
    new_node->data = data;
    new_node->next = head;
    new_node->prev = head->prev;

    head->prev->next = new_node;
    head->prev = new_node;
  }

  void display(struct list* head)
  {
    if(head == NULL || head->next == head)
    {
      fprintf(stderr,"empty\n");
      return;
    }

    struct list* tmp;
    tmp = head->next;
    while(tmp != head)
    {
      printf("%d ",tmp->data);
      tmp = tmp->next;
    }
    printf("\n");

  }

  void del_data(struct list* head,int data)
  {
    if(head == NULL || head->next == head)
    {
      fprintf(stderr,"empty\n");
      return;
    }
    struct list* tmp;
    tmp = head->next;
    while(tmp != head)
    {
      if(data == tmp->data)
      {
        break;
      }
      else
      {
        tmp = tmp->next;
      }
    }

    if(tmp == head)
    {
      fprintf(stderr,"no find\n");
      return;
    }

    tmp->next->prev = tmp->prev;
    tmp->prev->next = tmp->next;
    tmp->next = tmp->prev = NULL;
    free(tmp);
  }
  void del_list(struct list* head)
  {
    if(head == NULL || head->next == head)
    {
      fprintf(stderr,"empty\n");
      return;
    }

    struct list* tmp;
    struct list* prev;
    tmp = head->next;
    while(tmp != head)
    {
      prev = tmp;
      tmp = tmp->next;
      free(prev);
    }
  }
